﻿using CRMG.Container.Resources.ExceptionResources;
using CRMG.Container.Source.BO;
using CRMG.Container.Source.Helper;
using CRMG.Container.Source.OpManager;
using CRMG.Container.Source.Variables;
using Guru.ErrorHandling;
using Guru.Framework.Util;
using Guru.TransactionLogger;
using Guru.TransactionLogger.Source.BO;
using System;
using System.Data;
using System.Globalization;
using System.Resources;
using System.Threading;
using Telerik.WinControls.UI;

namespace CRMG.Container.Views.Lookup
{
    public partial class FrmCompany : RadForm
    {
        ResourceManager formRes;
        ResourceManager messageRes;
        LookupManager lookupMan;
        DataTable dtCompanies;
        int pCompId = 0;

        public FrmCompany()
            : this(-1)
        { }

        public FrmCompany(int companyId)
        {
            try
            {
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(AppVariables.ApplicationCulture);
                InitializeComponent();
                formRes = new ResourceManager("CRMG.Container.Resources.ControlResources.WinFormStrings", typeof(FrmCompany).Assembly);
                messageRes = new ResourceManager("CRMG.Container.Resources.MessageResources.AppMessages", typeof(FrmCompany).Assembly);
                lookupMan = new LookupManager();
                dtCompanies = lookupMan.GetCompanies();
                pCompId = companyId;
                this.Text = formRes.GetString("FrmCompany.Caption");
                this.lblCompany.Text = formRes.GetString("FrmCompany.lblCompany");
                this.btnSave.Text = formRes.GetString("Add");
                this.btnCancel.Text = formRes.GetString("Cancel");

                if (pCompId != -1)
                {
                    this.btnSave.Text = formRes.GetString("Update");
                    object obj = DataUtil.GetObjectWithSelectedColumn(dtCompanies, "CompanyName", pCompId, "OBJID");
                    this.txtCompany.Text = obj != null ? obj.ToString() : string.Empty;
                }
            }
            catch (GuruException)
            {
                ControlUtil.ShowErrorMessage(
                          messageRes.GetString("FrmCompany.Ctor.Error"),
                          messageRes.GetString("Error"));
            }
            catch (Exception exc)
            {
                LogEntry logger = new LogEntry(AppVariables.CurrentUser.OBJID)
                {
                    ErrorCode = 2040,
                    Message = exc.Message,
                    StackTrace = exc.StackTrace,
                    Title = Rc.GetString(2040)
                };
                logger.Write();
                ControlUtil.ShowErrorMessage(
                      messageRes.GetString("FrmCompany.Ctor.Error"),
                      messageRes.GetString("Error"));
            }
            finally
            {
                btnSave.Enabled = false;
            }
        }

        private void CompanyNameChange(object sender, EventArgs e)
        {
            btnSave.Enabled = txtCompany.Text.Trim().Length > 0;
        }

        private void Save(object sender, EventArgs e)
        {
            try
            {
                int CmpId = DataUtil.GetObjectWithSelectedColumn(dtCompanies, "CompanyName", txtCompany.Text, "OBJID").ToInt();
                if (CmpId != pCompId && CmpId > 0)
                {
                    ControlUtil.ShowWarningMessage(
                    messageRes.GetString("FrmCompany.Save.CompanyAlreadyExist"),
                    messageRes.GetString("Warning"));
                    return;
                }
                else
                {
                    Company cmpny = new Company() { CompanyName = txtCompany.Text };
                    int Id = 0;
                    if (pCompId == -1)
                    {
                        cmpny.IsActive = 1;
                        Id = lookupMan.AddCompany(cmpny);
                    }
                    else
                    {
                        cmpny.OBJID = pCompId;
                        lookupMan.UpdateCompany(cmpny);
                    }
                    TransactionLogEntry transLogger = new TransactionLogEntry(AppVariables.CurrentUser.OBJID)
                    {
                        FormName = this.Name,
                        TransactionType = pCompId == -1 ? TransactionTypes.Insert : TransactionTypes.Update,
                        ControlName = btnSave.Text,
                        ControlTag = btnSave.Tag.ToStr(),
                        LogObject = pCompId == -1 ? Id : pCompId
                    };
                    transLogger.Insert();

                    btnSave.Enabled = false;
                    btnCancel.Text = formRes.GetString("Close");
                    ControlUtil.ShowInfoMessage(
                      messageRes.GetString("FrmCompany.Save.Success"),
                      messageRes.GetString("Info"));
                }
            }
            catch (GuruException)
            {
                ControlUtil.ShowErrorMessage(
                          messageRes.GetString("FrmCompany.Save.Error"),
                          messageRes.GetString("Error"));
            }
            catch (Exception exc)
            {
                LogEntry logger = new LogEntry(AppVariables.CurrentUser.OBJID)
                {
                    ErrorCode = 2041,
                    Message = exc.Message,
                    StackTrace = exc.StackTrace,
                    Title = Rc.GetString(2041)
                };
                logger.Write();
                ControlUtil.ShowErrorMessage(
                      messageRes.GetString("FrmCompany.Save.Error"),
                      messageRes.GetString("Error"));
            }
        }

        private void Cancel(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
